package com.gem.web;

import com.gem.entity.Student;
import com.gem.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.gem.util.PageUtil.PAGESIZE;

/**
 * @author 刘聪
 * Created  on 2020/10/9
 */
@Controller
@RequestMapping("/student")
@SessionAttributes({"login", "message1"})
public class UserController {
    @Autowired
    StudentService studentService;

    @PostMapping("/login")
    @ResponseBody
    public String login(Model model, String name, String password) {
        Student login = studentService.login(name, password);
        System.out.println(login);
        if (login != null) {
            model.addAttribute("login", login);
//            System.out.println(login.toString());
            return "1";
        }
        return "0";
    }

    @RequestMapping("/tologin")
    public String tologin(Model model) {
        model.addAttribute("message1", "2");
        System.out.println("message1" + model.getAttribute("message1"));

        return "/";
    }

    @GetMapping("/index")
    public String index() {
        return "list";
    }

    @RequestMapping("/list")
    public String list(
            @RequestParam(defaultValue = "1", required = false) int pageNum,
            Model model) {
        PageInfo<Student> pageInfo = studentService.listPage(pageNum, PAGESIZE);
        //pageInfo中包含了  查询数据  分页  页码  总页码 所有页码数。。。。
        model.addAttribute("PageInfo", pageInfo);
        model.addAttribute("PageNum", pageNum);
        return "datas";
    }

    @RequestMapping("/search")
    public String search(@RequestParam(defaultValue = "1", required = false) int pageNum,
                         Model model,String name) {
        PageInfo<Student> pageInfo = studentService.searchByNamePages(pageNum, PAGESIZE,name);
        model.addAttribute("SearchPageInfo", pageInfo);
        model.addAttribute("SearchPageNum", pageNum);
//        System.out.println(pageInfo.getList());
        return "search";
    }

    @RequestMapping("/back")
    public String back() {
        return "redirect:/student/index";
    }
/*
@RequestMapping(value = "/preSearch/{name}")
    public String search(@RequestParam(defaultValue = "1", required = false) int pageNum,
                         Model model,@PathVariable String name) {
        PageInfo<Student> pageInfo = studentService.searchByNamePages(pageNum, PAGESIZE,name);
        model.addAttribute("SearchPageInfo", pageInfo);
        model.addAttribute("SearchPageNum", pageNum);
        System.out.println(pageInfo.getList());
        return "search";
    }
*/



    @PostMapping("/edit")
    public String edit(Student student) {
        studentService.editStudents(student);
        return "list";
    }

    @GetMapping("/preAdd")
    public String preAdd() {
        return "add";
    }

    @PostMapping("/add")
    @ResponseBody
    public String add(Student student) {
        String number = student.getStudentNumber();
        Student byNumber = studentService.searchStudentByNumber(number);
        if (byNumber == null) {
            studentService.addStudent(student);
            return "1";
        }
        return "0";
    }

    @GetMapping("/preEdit")
    public String preEdit(Model model, Long id) {
        Student studentById = studentService.searchStudentById(id);
        model.addAttribute("editStudent", studentById);
        return "edit";
    }

    @GetMapping("/showDetail")
    public String showDetail(Long id, Model model) {
        Student studentById = studentService.searchStudentById(id);
        model.addAttribute("stuId", studentById);
        return "detail";
    }

    @GetMapping("/delete")
    public String delete(Long id) {
        studentService.deleteStudents(id);
        return "list";
    }

    /*
     * 表单提交日期绑定
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(true);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }


    @GetMapping("/logout")
    public String logout(HttpSession session, SessionStatus sessionStatus, Model model) {
        session.invalidate();
        sessionStatus.setComplete();

        System.out.println("\"login\":" + model.getAttribute("login"));
        return "redirect:/";
    }
}
